﻿Public Class frmMain
    Dim state As Integer = 0
    Dim resetState As Integer = 0
    Dim iMSec As Integer
    Dim iSec As Integer
    Dim iMin As Integer
    Dim iHr As Integer
    Dim iDay As Integer
    Private Sub btnPlayPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlayPause.Click
        Select Case state
            Case 0 'If the timer is off.
                'set state to play (1)
                state = 1
                Timer.Start()
                btnPlayPause.Image = My.Resources.PauseHS
                If lblStart.Text = "" Then
                    lblStart.Text = "Timer started " & Date.Now()
                End If
            Case 1 'Running
                'set pause state 2
                state = 0
                Timer.Stop()
                btnReset.Enabled = True
                btnPlayPause.Image = My.Resources.PlayHS
                icoNotify.Text = "Stopwatch " & lblDisplay.Text & " (Paused)"
        End Select
    End Sub

    Private Sub btnStopReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Select Case state
            Case 0 'If the timer is off.
                'reset
                iSec = 0
                iMin = 0
                iHr = 0
                iDay = 0
                state = 0
                btnReset.Enabled = False
                btnPlayPause.Image = My.Resources.PlayHS
                lblStart.Text = ""
            Case 1 'If the timer is running
                'Do nothing.
        End Select
        lblDisplay.Text = iDay.ToString.PadLeft(2, "0"c) & ":" & iHr.ToString.PadLeft(2, "0"c) & ":" & iMin.ToString.PadLeft(2, "0"c) & ":" & iSec.ToString.PadLeft(2, "0"c)

        icoNotify.Text = "Stopwatch (Stopped)"
    End Sub

    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
        If state = 1 Then
            iMSec = 0
                iSec += 1
                If iSec = 60 Then
                    iSec = 0
                    iMin += 1
                    If iMin = 60 Then
                        iMin = 0
                        iHr += 1
                        If iHr = 24 Then
                            iHr = 0
                            iDay += 1
                        End If
                    End If
                End If
            End If
        lblDisplay.Text = iDay.ToString.PadLeft(2, "0"c) & ":" & iHr.ToString.PadLeft(2, "0"c) & ":" & iMin.ToString.PadLeft(2, "0"c) & ":" & iSec.ToString.PadLeft(2, "0"c)

        If lblDisplay.Text = "00:00:00:00" Then
            icoNotify.Text = "Stopwatch (Stopped)"
        Else
            icoNotify.Text = "Stopwatch (" & lblDisplay.Text & ")"
        End If
    End Sub

    Private Sub StartTimerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartTimerToolStripMenuItem.Click
        PauseTimerToolStripMenuItem.Enabled = True
        StartTimerToolStripMenuItem.Enabled = False
        ResetTimerToolStripMenuItem.Enabled = False
        btnPlayPause_Click(sender, e)
    End Sub

    Private Sub PauseTimerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PauseTimerToolStripMenuItem.Click
        StartTimerToolStripMenuItem.Enabled = True
        PauseTimerToolStripMenuItem.Enabled = False
        ResetTimerToolStripMenuItem.Enabled = True
        btnPlayPause_Click(sender, e)
    End Sub

    Private Sub ResetTimerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetTimerToolStripMenuItem.Click
        StartTimerToolStripMenuItem.Enabled = True
        ResetTimerToolStripMenuItem.Enabled = False
        btnStopReset_Click(sender, e)
    End Sub

    Private Sub AboutTimerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutTimerToolStripMenuItem.Click
        frmAbout.Show()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub frmMain_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        My.Settings.locationX = Me.Location.X
        My.Settings.locationY = Me.Location.Y
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'On first run, set location as centered.
        If My.Settings.firstRun Then
            Me.StartPosition = FormStartPosition.CenterScreen
            My.Settings.firstRun = False
        Else
            Me.Location = New Point(My.Settings.locationX, My.Settings.locationY)
        End If

        If My.Computer.Info.OSFullName.Contains("7") Then
            Me.ShowInTaskbar = True
        End If
    End Sub

    Private Sub icoNotify_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles icoNotify.DoubleClick
        Me.BringToFront()
    End Sub
End Class
